package com.captjack.common.util.math.map;

import com.captjack.common.util.math.LocationPoint;

/**
 * <p>
 * ----------------------------------------------------------------------------- <br>
 * project name ：micro-service <br>
 * function：地图工具 <br>
 * description：地图工具 <br>
 * ----------------------------------------------------------------------------- <br>
 * change history <br>
 * <table width="432" border="1">
 * <tr>
 * <td>version</td>
 * <td>time</td>
 * <td>author</td>
 * <td>change</td>
 * </tr>
 * <tr>
 * <td>1.0.0</td>
 * <td>2018/9/16 00:47</td>
 * <td>Jack Sparrow</td>
 * <td>create</td>
 * </tr>
 * </table>
 * <br>
 *
 * @author Jack Sparrow
 * @version 1.0.0
 * @date 2018/9/16 00:47
 * package com.captjack.common.util.math.map
 */
public final strictfp class MapUtils {

    private MapUtils() {

    }

    /**
     * 地球半径，单位：km
     */
    public static final double EARTH_RADIUS = 6378.137D;

    /**
     * 计算两个经纬度（点）的距离，单位：km
     *
     * @return 两个经纬度（点）的距离
     */
    public static double calculateDistanceBetweenTwoPoint(LocationPoint locationPoint1, LocationPoint locationPoint2) {
        return calculateDistanceBetweenTwoPoint(locationPoint1.getLongitude(), locationPoint1.getLatitude(), locationPoint2.getLongitude(), locationPoint2.getLatitude());
    }

    /**
     * 计算两个经纬度（点）的距离，单位：km
     *
     * @param lon1 第一个经度
     * @param lat1 第一个纬度
     * @param lon2 第二个经度
     * @param lat2 第二个纬度
     * @return 两个经纬度（点）的距离
     */
    public static double calculateDistanceBetweenTwoPoint(double lon1, double lat1, double lon2, double lat2) {
        double radLat1 = Math.toRadians(lat1);
        double radLat2 = Math.toRadians(lat2);
        double a = radLat1 - radLat2;
        double b = Math.toRadians(lon1) - Math.toRadians(lon2);
        return 2 * EARTH_RADIUS * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
    }

}
